home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / i_l / irit5 / geom_lib / bbox.c < prev    next >
C/C++ Source or Header  |  1995-12-30  |  11KB  |  246 lines

  1. /******************************************************************************
  2. * Bbox.c - computes bounding boxes for objects.                      *
  3. *******************************************************************************
  4. * Written by Gershon Elber, June 1993.                          *
  5. ******************************************************************************/
  6.  
  7. #include "irit_sm.h"
  8. #include "allocate.h"
  9. #include "cagd_lib.h"
  10. #include "bbox.h"
  11.  
  12. #define RESET_BBOX(Bbox) { Bbox.Min[0] = Bbox.Min[1] = Bbox.Min[2] = INFINITY;\
  13.              Bbox.Max[0] = Bbox.Max[1] = Bbox.Max[2] = -INFINITY; }
  14.  
  15. #define SET_IF_LESS_THAN(Val, NewVal)     { if (NewVal < Val) Val = NewVal; }
  16. #define SET_IF_GREATER_THAN(Val, NewVal)  { if (NewVal > Val) Val = NewVal; }
  17. #define SET_PT_IF_LESS_THAN(Pt, NewPt)    { SET_IF_LESS_THAN(Pt[0], NewPt[0]) \
  18.                         SET_IF_LESS_THAN(Pt[1], NewPt[1]) \
  19.                         SET_IF_LESS_THAN(Pt[2], NewPt[2]) }
  20. #define SET_PT_IF_GREATER_THAN(Pt, NewPt) \
  21.                        { SET_IF_GREATER_THAN(Pt[0], NewPt[0]) \
  22.                      SET_IF_GREATER_THAN(Pt[1], NewPt[1]) \
  23.                      SET_IF_GREATER_THAN(Pt[2], NewPt[2]) }
  24.  
  25. /*****************************************************************************
  26. * DESCRIPTION:                                                               M
  27. * Computes a bounding box of a given object of any type.             M
  28. *                                                                            *
  29. * PARAMETERS:                                                                M
  30. *   PObj:      To compute a bounding box for.                                M
  31. *                                                                            *
  32. * RETURN VALUE:                                                              M
  33. *   BBBboxStruct *:   A pointer to a statically allocated bounding box       M
  34. *                     holding bounding box information on PObj.              M
  35. *                                                                            *
  36. * KEYWORDS:                                                                  M
  37. *   BBComputeBboxObject, bounding box                                        M
  38. *****************************************************************************/
  39. BBBboxStruct *BBComputeBboxObject(IPObjectStruct *PObj)
  40. {
  41.     static BBBboxStruct Bbox;
  42.     int i;
  43.     IPObjectStruct *PObjTmp;
  44.     BBBboxStruct *PBbox;
  45.     CagdBBoxStruct CagdBbox;
  46.  
  47.     switch (PObj -> ObjType) {
  48.     case IP_OBJ_POLY:
  49.         return BBComputePolyListBbox(PObj -> U.Pl);
  50.     case IP_OBJ_POINT:
  51.         return BBComputePointBbox(PObj -> U.Pt);
  52.     case IP_OBJ_VECTOR:
  53.         return BBComputePointBbox(PObj -> U.Vec);
  54.     case IP_OBJ_LIST_OBJ:
  55.         RESET_BBOX(Bbox);
  56.         PBbox = &Bbox;
  57.         for (i = 0; (PObjTmp = ListObjectGet(PObj, i++)) != NULL; )
  58.         PBbox = BBMergeBbox(PBbox, BBComputeBboxObject(PObjTmp));
  59.         return PBbox;
  60.     case IP_OBJ_CURVE:
  61.         CagdCrvListBBox(PObj -> U.Crvs, &CagdBbox);
  62.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  63.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));
  64.         return &Bbox;
  65.     case IP_OBJ_SURFACE:
  66.         CagdSrfListBBox(PObj -> U.Srfs, &CagdBbox);
  67.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  68.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));    
  69.         return &Bbox;
  70.     case IP_OBJ_TRIMSRF:
  71.         CagdSrfListBBox(PObj -> U.TrimSrfs -> Srf, &CagdBbox);
  72.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  73.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));    
  74.         return &Bbox;
  75.     case IP_OBJ_TRIVAR:
  76.         TrivTVListBBox(PObj -> U.Trivars, &CagdBbox);
  77.         GEN_COPY(Bbox.Min, CagdBbox.Min, 3 * sizeof(RealType));
  78.         GEN_COPY(Bbox.Max, CagdBbox.Max, 3 * sizeof(RealType));    
  79.         return &Bbox;
  80.     default:
  81.         RESET_BBOX(Bbox);
  82.         return &Bbox;
  83.     }
  84. }
  85.  
  86. /*****************************************************************************
  87. * DESCRIPTION:                                                               M
  88. * Computes a bounding box of a list of objects of any type.             M
  89. *                                                                            *
  90. * PARAMETERS:                                                                M
  91. *   PObj:      To compute a bounding box for.                                M
  92. *                                                                            *
  93. * RETURN VALUE:                                                              M
  94. *   BBBboxStruct *:   A pointer to a statically allocated bounding box       M
  95. *                     holding bounding box information on objects PObj.      M
  96. *                                                                            *
  97. * KEYWORDS:                                                                  M
  98. *   BBComputeBboxObjectList, bounding box                                    M
  99. *****************************************************************************/
  100. BBBboxStruct *BBComputeBboxObjectList(IPObjectStruct *PObj)
  101. {
  102.     BBBboxStruct Bbox, *PBbox;
  103.  
  104.     RESET_BBOX(Bbox);
  105.     PBbox = &Bbox;
  106.  
  107.     for ( ; PObj != NULL; PObj = PObj -> Pnext) {
  108.     PBbox = BBMergeBbox(PBbox, BBComputeBboxObject(PObj));
  109.     }
  110.  
  111.     return PBbox;
  112. }
  113.  
  114. /*****************************************************************************
  115. * DESCRIPTION:                                                               M
  116. * Computes a bounding box of a polygon/polyline/pointlist object.         M
  117. *                                                                            *
  118. * PARAMETERS:                                                                M
  119. *   PPoly:     To compute a bounding box for.                                M
  120. *                                                                            *
  121. * RETURN VALUE:                                                              M
  122. *   BBBboxStruct *:   A pointer to a statically allocated bounding box       M
  123. *                     holding bounding box information on PPoly.             M
  124. *                                                                            *
  125. * KEYWORDS:                                                                  M
  126. *   BBComputeOnePolyBbox, bounding box                                       M
  127. *****************************************************************************/
  128. BBBboxStruct *BBComputeOnePolyBbox(IPPolygonStruct *PPoly)
  129. {
  130.     static BBBboxStruct Bbox;
  131.     IPVertexStruct
  132.     *V = PPoly -> PVertex;
  133.  
  134.     RESET_BBOX(Bbox);
  135.  
  136.     do {
  137.     SET_PT_IF_LESS_THAN(Bbox.Min, V -> Coord);
  138.     SET_PT_IF_GREATER_THAN(Bbox.Max, V -> Coord);
  139.     V = V -> Pnext;
  140.     }
  141.     while (V != NULL && V != PPoly -> PVertex);
  142.  
  143.     return &Bbox;
  144. }
  145.  
  146. /*****************************************************************************
  147. * DESCRIPTION:                                                               M
  148. * Computes a bounding box for a list of polygon/polyline/pointlist objects.  M
  149. *                                                                            *
  150. * PARAMETERS:                                                                M
  151. *   PPoly:     To compute a bounding box for.                                M
  152. *                                                                            *
  153. * RETURN VALUE:                                                              M
  154. *   BBBboxStruct *:   A pointer to a statically allocated bounding box       M
  155. *                     holding bounding box information on PPoly list.        M
  156. *                                                                            *
  157. * KEYWORDS:                                                                  M
  158. *   BBComputeOnePolyListBbox, bounding box                                   M
  159. *****************************************************************************/
  160. BBBboxStruct *BBComputePolyListBbox(IPPolygonStruct *PPoly)
  161. {
  162.     static BBBboxStruct Bbox;
  163.  
  164.     RESET_BBOX(Bbox);
  165.  
  166.     for ( ; PPoly != NULL; PPoly = PPoly -> Pnext) {
  167.     IPVertexStruct
  168.         *V = PPoly -> PVertex;
  169.  
  170.     do {
  171.         SET_PT_IF_LESS_THAN(Bbox.Min, V -> Coord);
  172.         SET_PT_IF_GREATER_THAN(Bbox.Max, V -> Coord);
  173.         V = V -> Pnext;
  174.     }
  175.     while (V != NULL && V != PPoly -> PVertex);
  176.     }
  177.  
  178.     return &Bbox;
  179. }
  180.  
  181. /*****************************************************************************
  182. * DESCRIPTION:                                                               M
  183. * Computes a bounding box of a point object.                                 M
  184. *                                                                            *
  185. * PARAMETERS:                                                                M
  186. *   Pt:     To compute a bounding box for.                                   M
  187. *                                                                            *
  188. * RETURN VALUE:                                                              M
  189. *   BBBboxStruct *:   A pointer to a statically allocated bounding box       M
  190. *                     holding bounding box information on Pt.                M
  191. *                                                                            *
  192. * KEYWORDS:                                                                  M
  193. *   BBComputePointBbox, bounding box                                         M
  194. *****************************************************************************/
  195. BBBboxStruct *BBComputePointBbox(RealType *Pt)
  196. {
  197.     static BBBboxStruct Bbox;
  198.  
  199.     PT_COPY(Bbox.Min, Pt);
  200.     PT_COPY(Bbox.Max, Pt);
  201.  
  202.     return &Bbox;
  203. }
  204.  
  205. /*****************************************************************************
  206. * DESCRIPTION:                                                               M
  207. * Merges (union) given two bounding boxes into one.                 M
  208. * Either Bbox1 or Bbox2 can be pointing to the static area Bbox used herein. M
  209. *                                                                            *
  210. * PARAMETERS:                                                                M
  211. *   Bbox1:      First bounding box to union up.                              M
  212. *   Bbox2:      Second bounding box to union up.                             M
  213. *                                                                            *
  214. * RETURN VALUE:                                                              M
  215. *   BBBboxStruct *:    A unioned bounding box the contains both BBox1 and    M
  216. *                      BBox2.                                                M
  217. *                                                                            *
  218. * KEYWORDS:                                                                  M
  219. *   BBMergeBbox, bounding box                                                M
  220. *****************************************************************************/
  221. BBBboxStruct *BBMergeBbox(BBBboxStruct *Bbox1, BBBboxStruct *Bbox2)
  222. {
  223.     static BBBboxStruct Bbox;
  224.     int i;
  225.  
  226.     /* Make sure first Bbox is in Bbox and Bbox2 holds the other one. */
  227.     if (Bbox1 == &Bbox) {
  228.     }
  229.     if (Bbox2 == &Bbox) {
  230.     Bbox2 = Bbox1;
  231.     }
  232.     else {
  233.     GEN_COPY(&Bbox, Bbox1, sizeof(BBBboxStruct));
  234.     }
  235.  
  236.     /* Compare the two Bbox's and update. */
  237.     for (i = 0; i < 3; i++) {
  238.     if (Bbox.Min[i] > Bbox2 -> Min[i])
  239.         Bbox.Min[i] = Bbox2 -> Min[i];
  240.     if (Bbox.Max[i] < Bbox2 -> Max[i])
  241.         Bbox.Max[i] = Bbox2 -> Max[i];
  242.     }
  243.  
  244.     return &Bbox;
  245. }
  246.